import Vue from 'vue';
import Vuex from 'vuex';
// import getMenu from "./getMenu";
import {Message} from 'element-ui';
import getters from './getters';
import webSetInfo from "../server/webSetInfo";
import {axios} from '../server/axios';
import {axios_token} from '../server/axios_token';
import {CommonRequest} from "../server/request";

Vue.use(Vuex)

const modulesFiles = require.context('./modules', true, /\.js$/)
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
    const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
    const value = modulesFiles(modulePath)
    modules[moduleName] = value.default
    return modules
}, {})
export default new Vuex.Store({

    state: {
        title: "",                              //系统名称
        serverUrl: "",
        menu: '',
        username:"",
        companyCode:"",
        userLoginStatus: 'false' ,//用户登录状态
        userAllInfos: {},//用户信息
        userMenus: [],//用户菜单,
        companyInfos: [],//公司信息
        roleInfos: []//角色信息
    },
    mutations: {
        //修改用户登录状态
        setUserLoginStatus(state,payload){
            state.userLoginStatus = payload.status
            localStorage.setItem('userStatus',payload.status)
        },
        //修改用户的数据
        setUserAllInfos(state,payload){
            state.userAllInfos = payload.content
            localStorage.setItem('userAllInfos',JSON.stringify(payload.content))
        },
        //修改用户的菜单权限
        setUserMenus(state,payload){
            state.userMenus = payload
            localStorage.setItem('userMenus',JSON.stringify(payload))
        },
        //公司信息
        setCompanyInfos(state, payload) {
            state.companyInfos = payload
            localStorage.setItem('companyInfos', JSON.stringify(payload.context))
        },
        setRoleInfos(state, payload) {
            state.roleInfos = payload
            localStorage.setItem('roleInfos', JSON.stringify(payload.context))
        },
        SET_MENU: (state, menu) => {
            state.menu = menu
        },
        SET_TITLE: (state, title) => {
            state.title = title
        },
        SET_MENU_CURRENT: (state, info) => {
            state.menuCurrent = [...info]
        },
        SET_MENU_TYPE: (state, type) => {
            state.menuType = type
        },
        SET_USER_NAME: (state, name) => {
            state.username = name
        },
        SET_COMPANY_CODE:(state, code) => {
            state.companyCode = code
        },
        SET_USER_CENTERX:(state, centerX) => {
            state.centerX = centerX
        },
        SET_USER_CENTERY:(state, centerY) => {
            state.centerY = centerY
        },
        SET_USER_STREET:(state, streetStatus) => {
            state.streetStatus = streetStatus
        }
    },
    actions: {
        //用户登录
        userLogin(context,payload){
            return new Promise( (resolve, reject) => {
                axios.post (`${webSetInfo.apiUrl}` + `/user/login`, {
                    username: payload.username,
                    password: payload.password,
                    account: payload.account
                })
                    .then(res => {
                        let result = res.code;
                        if (result === 200) {
                            this.userInfo = res.data;
                            context.commit("setUserAllInfos", {content: this.userInfo});
                            // this.getRouter(context);
                            // let menus =  res.hiddenParam.menu.children ? res.hiddenParam.menu.children : []
                            // console.log('权限菜单',getMenu(menus))
                            // context.commit('setUserMenus',{menus: getMenu(menus)})
                        }
                        resolve(res)
                    }).catch(err => {
                        Message.error(err);
                })
            })
        },

        getRouter({commit}) {
            axios_token.post(`${webSetInfo.apiUrl}` + `/sys/getRouters`, {}).then(res => {
                let data = res.data;
                if (data) {
                    commit("setUserMenus", data);
                }
            })
        },
        // 获取用户名
        // getUserName({commit}, state){
        //     axios.get("/power/getCurrentUser")
        //         .then(res => {
        //             commit("SET_USER_NAME", res.name)
        //             commit("SET_COMPANY_CODE", res.companyCode)
        //             commit("SET_USER_CENTERX",res.centerX)
        //             commit("SET_USER_CENTERY",res.centerY)
        //             commit("SET_USER_STREET",res.streetStatus)
        //         })
        // },

        //设置当前菜单
        // setMenuCurrent ({commit, state}, type) {
        //     commit('SET_MENU_TYPE', type)
        //     let temp = state.menu.reduce((temp, item) => {
        //         if (item.type === type) {
        //             item.children = item.children.map(child => {
        //                 child.originType = item.originType
        //                 return child
        //             })
        //             temp = [...temp, ...item.children]
        //         }
        //         return temp
        //     }, [])
        //     commit('SET_MENU_CURRENT', temp)
        // },
        //改变菜单
        changeMenu ({commit, state}) {

        },
        // 改变标题
        changeTitle ({commit}, state) {
            // axios.get('/power/getBannerByUserId')
            //     .then(res => {
            //         commit('SET_TITLE', res)
            //     })
            //     .catch(err => {
            //     })
        }
    },
    getters,
    modules: modules
})


